package com.cndw.kungfu.model.goods.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.domain.Goods;

/**
 * 
 * 强化（星级）成套加成
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-5-30
 */
public class ModeGoodsStarSuit implements Cloneable {

	/**
	 * 星级
	 */
	private int level;

	/**
	 * 加血
	 */
	private int hp;
	/**
	 * 加魔法
	 */
	private int mp;

	/**
	 * 加攻击
	 */
	private int attack;

	/**
	 * 加防御
	 */
	private int defense;

	/**
	 * 命中
	 */
	private int hit;
	/**
	 * 闪避
	 */
	private int dodge;
	/**
	 * 暴击
	 */
	private int crit;
	/**
	 * 韧性
	 */
	private int tough;

	/**
	 * 是否激活
	 */
	private boolean actived;

	/**
	 * 当前拥有数
	 */
	private int hasCounts;

	private static Map<Integer, ModeGoodsStarSuit> suit = new HashMap<Integer, ModeGoodsStarSuit>();

	static {
		suit.put(3, new ModeGoodsStarSuit(3, 5000, 5000, 3000, 3000, 250, 250, 250, 250));
		suit.put(5, new ModeGoodsStarSuit(5, 15000, 15000, 8000, 8000, 1000, 1000, 1000, 1000));
		suit.put(7, new ModeGoodsStarSuit(7, 35000, 35000, 14000, 14000, 1500, 1500, 1500, 1500));
		suit.put(9, new ModeGoodsStarSuit(9, 50000, 50000, 20000, 20000, 2000, 2000, 2000, 2000));
		suit.put(11, new ModeGoodsStarSuit(11, 80000, 80000, 28000, 28000, 3000, 3000, 3000, 3000));
	}

	private ModeGoodsStarSuit(int level, int hp, int mp, int attack, int defense, int hit, int dodge, int crit, int tough) {
		this.level = level;
		this.hp = hp;
		this.mp = mp;
		this.attack = attack;
		this.defense = defense;
		this.hit = hit;
		this.dodge = dodge;
		this.crit = crit;
		this.tough = tough;
	}

	public ModeGoodsStarSuit() {

	}

	public static List<ModeGoodsStarSuit> getStarSuite(Collection<Goods> goodList) {

		// 初始化计数
		int m = 0;
		int[][] temp = new int[suit.keySet().size()][2];
		for (Integer level : suit.keySet()) {
			temp[m][0] = level;
			temp[m][1] = 0;
			m++;
		}

		for (Goods g : goodList) {
			for (int i = 0, j = temp.length; i < j; i++) {
				if (g.getLevel() >= temp[i][0]) {
					temp[i][1]++;
				} else {
					break;
				}
			}
		}

		List<ModeGoodsStarSuit> list = new ArrayList<ModeGoodsStarSuit>();
		try {
			for (int i = 0, j = temp.length; i < j; i++) {
				ModeGoodsStarSuit s = (ModeGoodsStarSuit) suit.get(temp[i][0]).clone();
				if (temp[i][1] >= 9) {
					s.setActived(true);
				} else {
					s.setActived(false);
				}
				s.setHasCounts(temp[i][1]);
				list.add(s);
			}
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static int getMaxStar(Collection<Goods> goodList) {
		int goodSize = goodList.size();
		if (goodSize < 9) {
			return 0;
		}
		int[] temp = new int[12];

		for (Goods g : goodList) {
			if (g.getGoodsMode() == null || g.getGoodsMode().isNOForgeEquiq()) {// 护符和婚戒不参与计算星级
				goodSize--;
				continue;
			}
			temp[g.getLevel()]++;
		}
		if (goodSize < 9) {// 不足9件装备
			return 0;
		}
		for (int i = 0; i < temp.length; i++) {
			if (temp[i] > 0) {
				return i;
			}
		}
		return 0;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getLevel() {
		return level;
	}

	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public int getMp() {
		return mp;
	}

	public void setMp(int mp) {
		this.mp = mp;
	}

	public int getAttack() {
		return attack;
	}

	public void setAttack(int attack) {
		this.attack = attack;
	}

	public int getDefense() {
		return defense;
	}

	public void setDefense(int defense) {
		this.defense = defense;
	}

	public boolean isActived() {
		return actived;
	}

	public void setActived(boolean actived) {
		this.actived = actived;
	}

	public int getHit() {
		return hit;
	}

	public void setHit(int hit) {
		this.hit = hit;
	}

	public int getDodge() {
		return dodge;
	}

	public void setDodge(int dodge) {
		this.dodge = dodge;
	}

	public int getCrit() {
		return crit;
	}

	public void setCrit(int crit) {
		this.crit = crit;
	}

	public int getTough() {
		return tough;
	}

	public void setTough(int tough) {
		this.tough = tough;
	}

	public int getHasCounts() {
		return hasCounts;
	}

	public void setHasCounts(int hasCounts) {
		this.hasCounts = hasCounts;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return new ModeGoodsStarSuit(level, hp, mp, attack, defense, hit, dodge, crit, tough);
	}
}
